home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / system / mail / transpor / ifmail23.z / ifmail23 / ifmail / ifgate / flock.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-04  |  865 b   |  67 lines

  1. #include <sys/types.h>
  2. #include <unistd.h>
  3. #include <stdio.h>
  4. #include <sys/stat.h>
  5. #include <fcntl.h>
  6. #include <errno.h>
  7. #include "lutil.h"
  8.  
  9. int f_lock(fn)
  10. char *fn;
  11. {
  12.     int lfd=-1;
  13.     struct flock fl = {
  14.         F_WRLCK,
  15.         0,
  16.         0L,
  17.         0L,
  18.         0
  19.     };
  20.     struct stat st;
  21.  
  22.     if (fn)
  23.     {
  24.         if ((lfd=open(fn,O_RDWR | O_CREAT)) < 0)
  25.         {
  26.             logerr("$Error opening file %s",fn);
  27.             return -1;
  28.         }
  29.         fl.l_pid=getpid();
  30.         if (fcntl(lfd,F_SETLK,&fl) != 0)
  31.         {
  32.             if (errno != EAGAIN)
  33.                 loginf("$Error locking file %s",fn);
  34.             close(lfd);
  35.             return -1;
  36.         }
  37.         if (stat(fn,&st) != 0)
  38.         {
  39.             logerr("$Error accessing file %s",fn);
  40.             close(lfd);
  41.             return -1;
  42.         }
  43.     }
  44.     return lfd;
  45. }
  46.  
  47. void funlock(fd)
  48. int fd;
  49. {
  50. /*
  51.     struct flock fl = {
  52.         F_UNLCK,
  53.         0,
  54.         0L,
  55.         0L,
  56.         0
  57.     };
  58.     fl.l_pid=getpid();
  59.     if (fcntl(fd,F_SETLK,&fl) != 0)
  60.     {
  61.         logerr("$Error ulocking fd %d",fd);
  62.     }
  63. */
  64.     close(fd);
  65.     return;
  66. }
  67.